package com.gl.handler;

import com.gl.common.utils.Result;
import com.gl.service.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Description:api全局异常处理器</p>
 * @author gaol
 * @version 1.0
 * @date 2017/5/12
 */
/*@ControllerAdvice*/
@RestControllerAdvice
public class CustomExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);


    @ExceptionHandler(value = ServiceException.class)
    //@ResponseBody
    public Result handleServiceException(HttpServletRequest request, ServiceException se) {
        logger.error("ServiceException:{}", se.getMessage());
        return new Result(se.getErrorCode().code, se.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    //@ResponseBody
    public Result handleException(HttpServletRequest request, Exception e) {
        logger.error("Exception:{}", e.getMessage());
        return new Result(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

}
